package com.xyx.bloodoxygenjava.utils;

import android.bluetooth.BluetoothAdapter;
import android.content.Context;
import android.os.Build;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/* loaded from: BDETrans.jar:com/creative/bluetooth/ble/BLEOpertion.class */
public class BLEOpertion implements ACSUtility.IACSUtilityCallback {
    public ACSUtility acsUtility;
    private IBLECallBack callBack;
    public Context context;
    private boolean isReady = false;
    public List<byte[]> readData = new ArrayList();
    private boolean BLEING = false;
    private List<ACSUtility.blePort> bluetoothList = new ArrayList();

    public static boolean isCanUseBLE(Context context) {
        if (Build.VERSION.SDK_INT < 18) {
            return false;
        }
        return context.getPackageManager().hasSystemFeature("android.hardware.bluetooth_le");
    }

    public BLEOpertion(Context context, IBLECallBack _callBack) throws Exception {
        if (context == null || _callBack == null) {
            throw new NullPointerException("context or callBack is NULL");
        }
        this.context = context;
        if (isCanUseBLE(context)) {
            this.acsUtility = new ACSUtility(context, this);
            this.callBack = _callBack;
            return;
        }
        throw new Exception("Can't use BLE");
    }

    public void startDiscover() {
        if (this.acsUtility != null && this.isReady && !this.BLEING) {
            this.acsUtility.enumAllPorts(10.0f);
            this.BLEING = true;
            if (this.bluetoothList != null) {
                this.bluetoothList.clear();
            }
        }
    }

    public void stopDiscover() {
        if (this.BLEING) {
            this.acsUtility.stopEnum();
            this.BLEING = false;
        }
    }

    public void writeData(byte[] value) {
        if (this.acsUtility != null) {
            this.acsUtility.writePort(value);
        }
    }

    public void connect(ACSUtility.blePort device) {
        if (this.acsUtility != null && !this.acsUtility.isPortOpened(device)) {
            this.acsUtility.openPort(device);
        }
    }

    public void connect(String address) {
        if (!BluetoothAdapter.checkBluetoothAddress(address)) {
            this.callBack.onConnectFail();
        }
        if (this.acsUtility != null) {
            this.acsUtility.openPort(address);
        }
    }

    public void disConnect() {
        if (this.acsUtility != null) {
            this.acsUtility.closePort();
        }
    }

    public void closeACSUtility() {
        if (this.acsUtility != null) {
            this.acsUtility.closeACSUtility();
        }
    }

    public boolean isConnect(ACSUtility.blePort arg0) {
        return this.acsUtility.isPortOpened(arg0);
    }

    @Override // com.bde.parentcyTransport.ACSUtility.IACSUtilityCallback
    public void didClosePort(ACSUtility.blePort arg0) {
        this.callBack.onDisConnect(arg0);
    }

    @Override // com.bde.parentcyTransport.ACSUtility.IACSUtilityCallback
    public void didFinishedEnumPorts() {
        this.callBack.onDiscoveryCompleted(this.bluetoothList);
        this.BLEING = false;
    }

    @Override // com.bde.parentcyTransport.ACSUtility.IACSUtilityCallback
    public void didFoundPort(ACSUtility.blePort arg0) {
        this.bluetoothList.add(arg0);
        this.callBack.onFindDevice(arg0);
    }

    @Override // com.bde.parentcyTransport.ACSUtility.IACSUtilityCallback
    public void didOpenPort(ACSUtility.blePort arg0, Boolean arg1) {
        if (arg1.booleanValue()) {
            this.callBack.onConnected(arg0);
        } else {
            this.callBack.onConnectFail();
        }
    }

    @Override // com.bde.parentcyTransport.ACSUtility.IACSUtilityCallback
    public void didPackageReceived(ACSUtility.blePort arg0, byte[] arg1) {
        this.readData.add(arg1);
    }

    @Override // com.bde.parentcyTransport.ACSUtility.IACSUtilityCallback
    public void didPackageSended(boolean arg0) {
        this.callBack.onSended(arg0);
    }

    @Override // com.bde.parentcyTransport.ACSUtility.IACSUtilityCallback
    public void heartbeatDebug() {
    }

    @Override // com.bde.parentcyTransport.ACSUtility.IACSUtilityCallback
    public void utilReadyForUse() {
        this.isReady = true;
        this.callBack.onReadyForUse();
    }

    public void cleanData() {
        this.readData.clear();
    }

    public int available() {
        if (this.readData != null) {
            return this.readData.size();
        }
        return 0;
    }

    public int read(byte[] buffer) throws IOException {
        int cnt = 0;
        while (this.readData.size() <= 0) {
            cnt++;
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            if (this.readData.size() <= 0) {
                if (cnt >= 500) {
                    break;
                }
            } else {
                break;
            }
        }
        if (this.readData.size() > 0) {
            int i = 0;
            int j = 0;
            while (this.readData.size() > 0) {
                byte[] buf = this.readData.remove(0);
                if (buf != null) {
                    i = j;
                    while (i < buf.length) {
                        buffer[i] = buf[i];
                        i++;
                    }
                    j = i;
                }
            }
            return i;
        }
        throw new IOException("there is no data to read");
    }
}